package com.teame.readbaby;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.teame.readbaby.parser.UserInfoParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.GloableParams;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.Userinfo;

public class LoginActivity extends BaseActivity {
	private EditText et_login_name_edit;
	private EditText et_login_pwd_edit;

	private String inputEmail;
	private String inputPassword;
	
	private TextView tv_login_login; // 登录按钮
	private TextView tv_login_regist; // 注册按钮按钮

	@Override
	protected void onClickEvent(View paramView) {
		if (paramView == tv_login_login) {
			if(checkInput()){ //用户输入正确
				String url=ConstantValue.HOST+ConstantValue.LOGIN;
				Map<String,String> paramMap=new HashMap<String, String>();
				paramMap.put("username", inputEmail);
				paramMap.put("password", inputPassword);
				this.getDataFromServer(url, paramMap, new UserInfoParser(),new DataCallback<Userinfo>() {
					@Override
					public void processData(Userinfo userInfo, boolean paramBoolean) {
						if (!paramBoolean || userInfo == null) {// 获取失败
							Toast.makeText(context, "用户名或密码错误", Toast.LENGTH_SHORT).show();
							return ;
						} else {// 获取成功
							GloableParams.userInfo=userInfo;
							Intent intent=new Intent(LoginActivity.this,AccountManageActivity.class);
							startActivity(intent);
							finish();
						}
					}
					
				});
			}else{  //用户输入错误
				//do nothing
			}
		} else if (paramView == tv_login_regist) {
			Logger.i(TAG, "注册按钮被点击");
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivity(intent);
		}
	}

	private boolean checkInput() {
		// 检查邮箱
		inputEmail = et_login_name_edit.getText().toString().trim();
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(inputEmail);
		if (!m.matches()) {
			et_login_name_edit.setText("");
			Toast.makeText(context, "请输入正确的邮箱地址", Toast.LENGTH_SHORT).show();
			return false;
		}
		// 检查密码
		inputPassword = et_login_pwd_edit.getText().toString().trim();
		if (inputPassword != null && !"".equals(inputPassword)) {
			return true;
		} else {
			et_login_pwd_edit.setText("");
			Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT)
					.show();
			return false;
		}

	}

	@Override
	protected void findViewById() {
		et_login_name_edit = (EditText) findViewById(R.id.et_login_name_edit);
		et_login_pwd_edit = (EditText) findViewById(R.id.et_login_pwd_edit);
		tv_login_login = (TextView) findViewById(R.id.tv_login_login);
		tv_login_regist = (TextView) findViewById(R.id.tv_login_regist);
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.login_activity);
	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		tv_login_login.setOnClickListener(this);
		tv_login_regist.setOnClickListener(this);
	}

}
